﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

namespace compress
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void kais_Click(object sender, EventArgs e)
        {
            //获取目录下所有文件
            String path = this.path1.Text;
            //控制文件名
            int i = 0;
            try { 
                i = Int32.Parse(this.textBox2.Text.Trim());
            }
            catch (Exception ee)
            {
                i = 0;
                this.textBox1.AppendText("\r\n"+ee);
            }
            //当前执行数
            int j = 0;
            DirectoryInfo root = new DirectoryInfo(path);
            FileInfo[] files= root.GetFiles();
            ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
            foreach (FileInfo subDir in files)
            {
                try
                {
                    Image img = Image.FromFile(this.path1.Text + "/" + subDir.Name);
                    img = ZoomImage(img, 1600, 1600);
                    //设置压缩质量       
                    System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
                    long[] quality = new long[1];
                    quality[0] = 90;
                    System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                    encoderParams.Param[0] = encoderParam;
                    img.Save(this.path2.Text + "/" + this.textBox3.Text.Trim()+(i++)+".png", jgpEncoder, encoderParams);
                    ////不设置压缩图片质量
                    //img.Save(this.path2.Text + "/" + "test" + (i++) + ".png");
                    //释放资源
                    img.Dispose();
                    img = null;
                    encoderParam.Dispose();
                    encoderParam = null;
                    encoderParams.Dispose();
                    encoderParams = null;
                    j++;
                    this.textBox1.AppendText("\r\n"+files.Length+" / "+j);
                }
                catch (Exception es)
                {
                    this.count.Text= es.Message;
                    Console.WriteLine(es.Message);
                }
                
            }
            this.count.Text = "success:"+j;
        }

        private static ImageCodecInfo GetEncoder(ImageFormat format)
        {
            // 获取所有支持的图像编码器
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
            // 找到第一个支持指定格式的编码器
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                {
                    return codec;
                }
            }
            // 如果没有找到匹配的编码器则抛出异常
            throw new ArgumentException("No appropriate encoder found.", nameof(format));
        }
        /// <summary>
        /// 不管多大的图片都能在指定大小picturebox控件中显示
        /// </summary>
        /// <param name="bitmap">图片</param>
        /// <param name="destHeight">picturebox控件高</param>
        /// <param name="destWidth">picturebox控件宽</param>
        /// <returns></returns>
        private Image ZoomImage(Image bitmap, int destHeight, int destWidth)
        {
            try
            {
                System.Drawing.Image sourImage = bitmap;
                int width = 0, height = 0;
                //按比例缩放             
                int sourWidth = sourImage.Width;
                int sourHeight = sourImage.Height;
                if (sourHeight > destHeight || sourWidth > destWidth)
                {
                    if ((sourWidth * destHeight) > (sourHeight * destWidth))
                    {
                        width = destWidth;
                        height = (destWidth * sourHeight) / sourWidth;
                    }
                    else
                    {
                        height = destHeight;
                        width = (sourWidth * destHeight) / sourHeight;
                    }
                }
                else
                {
                    width = sourWidth;
                    height = sourHeight;
                }

               // System.Drawing.Image thumbnailImage = sourImage.GetThumbnailImage(width, height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
                //System.Drawing.Bitmap bm = new System.Drawing.Bitmap(thumbnailImage);
                System.Drawing.Bitmap bm = resizeImage(sourImage,new Size(width,height));
                sourImage.Dispose();
                sourImage = null;
                //thumbnailImage.Dispose();
                //thumbnailImage = null;
                return bm;
            }
            catch (Exception ex)
            {
                return bitmap;
            }
        }

        public Bitmap resizeImage(Image imgToResize, Size size)
        {
            return new Bitmap(imgToResize, size);
        }
        private  bool ThumbnailCallback()
        {
            return false;
        }
    }
}
